package com.stylefeng.guns.modular.system.task;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.stylefeng.guns.core.common.constant.state.KpMethod;
import com.stylefeng.guns.core.util.HttpsClient;
import com.stylefeng.guns.modular.system.constants.ImportContants;
import com.stylefeng.guns.modular.system.dao.GpDashboardDetailMapper;
import com.stylefeng.guns.modular.system.dao.GpDashboardMapper;
import com.stylefeng.guns.modular.system.dao.ParamsMapper;
import com.stylefeng.guns.modular.system.model.GpDashboard;
import com.stylefeng.guns.modular.system.model.GpDashboardDetail;
import com.stylefeng.guns.modular.system.model.GpStatData;
import com.stylefeng.guns.modular.system.model.GpStatDetailData;
import com.stylefeng.guns.modular.system.model.Params;

@Service
public class PlatImportTask {
	
	@Autowired
	private ParamsMapper paramsMapper;
	
	@Autowired
	private GpDashboardMapper dashboardMapper;
	
	@Autowired
	private GpDashboardDetailMapper dashboardDetailMapper;
	
	public void execute(boolean isHoliday) {
		if(isHoliday){
			Params params = new Params();
			params.setgUserid("387065");
			params = paramsMapper.selectOne(params);
			if(params == null){
				return;
			}
			Hashtable<String, String> paramsMaps = new Hashtable<String, String>();
			paramsMaps.put("a", KpMethod.E.getMethod());
			paramsMaps.put("c", "PCArrangeData");
			paramsMaps.put("StockID", "SH000001");
			paramsMaps.put("st", "3");
			paramsMaps.put("Token", params.getgToken());
			paramsMaps.put("UserID", params.getgUserid());
			String result = HttpsClient.postSSL(ImportContants.getKpDataPathStr(), paramsMaps,true);
			if(StringUtils.isNotEmpty(result)){
				List<GpStatData> gpStatDatas  = JSON.parseArray(JSON.parseObject(result).getJSONObject("zs").getString("data"),GpStatData.class);
				for(GpStatData gpStatData : gpStatDatas){
					GpDashboard dashboard = new GpDashboard();
					dashboard.setgCode(gpStatData.getCode());
					dashboard.setgDate(new Date());
					dashboard = dashboardMapper.selectOneByDate(dashboard);
					if(dashboard == null){
						dashboard = new GpDashboard();
						dashboard.setgCode(gpStatData.getCode());
						dashboard.setgDate(new Date());
						dashboard.setgName(gpStatData.getName());
						dashboard.setgLastPx(gpStatData.getLast_px());
						dashboard.setgPxChange(gpStatData.getPx_change());
						dashboard.setgPxChangeRate(gpStatData.getPx_change_rate());
						dashboardMapper.insert(dashboard);
					}else{
						dashboard.setgName(gpStatData.getName());
						dashboard.setgLastPx(gpStatData.getLast_px());
						dashboard.setgPxChange(gpStatData.getPx_change());
						dashboard.setgPxChangeRate(gpStatData.getPx_change_rate());
						dashboardMapper.updateById(dashboard);
					}
				}
				List<GpStatDetailData> gpStatDetailDatas  = JSON.parseArray(JSON.parseObject(result).getJSONObject("point").getString("list"),GpStatDetailData.class);
				String hs = JSON.parseObject(result).getJSONObject("Mood").getString("SZJS");
				for(GpStatDetailData gpStatDetailData : gpStatDetailDatas){
					GpDashboardDetail dashboardDetail = new GpDashboardDetail();
					Date gTime = null;
					if(hs.length() == 4){
						gTime = new Date(Long.valueOf(gpStatDetailData.getTime() + "".concat(StringUtils.substring(hs,0, hs.length()-1))));
					}else{
						gTime = new Date(Long.valueOf(gpStatDetailData.getTime() + "".concat(hs + "")));
					}
					dashboardDetail.setgTime(gTime);
					dashboardDetail = dashboardDetailMapper.selectOneByDate(dashboardDetail);
					if(dashboardDetail == null){
						dashboardDetail = new GpDashboardDetail();
						dashboardDetail.setgTime(gTime);
						dashboardDetail.setgPlate(gpStatDetailData.getPlate());
						dashboardDetailMapper.insert(dashboardDetail);
					}
				}
			}
		}
	}
}
